package com.springboot.service.impl;

import com.springboot.dto.PatientChatIndex;
import com.springboot.mapper.ChatRecordMapper;
import com.springboot.mapper.UserLoginFormMapper;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.PatientChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class PatientChatServiceImpl implements PatientChatService {
    @Autowired
    ChatRecordMapper chatRecordMapper;

    @Autowired
    UserLoginFormMapper userLoginFormMapper;


    /**
     * @ description:显示患者的聊天列表
     * @ param      :[patientId]
     * @ return     :java.util.ArrayList<com.springboot.dto.PatientChatIndex>
     * @ date       :2021/1/3

    */
    @Override
    public ArrayList<PatientChatIndex> showChatIndex(Integer patientId) {
        ArrayList<ChatRecord> chatRecords = new ArrayList<>();
        try {
            chatRecords = chatRecordMapper.selectByPatientId(patientId);
            System.out.println(chatRecords.size());/////////////////////////
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 出错");
        }
        //把列表iD去重
        Integer userId[] = new Integer[100];
        int num=0;
//        if(chatRecords.size()==0)
//        {
//            return null;
//        }
        for(int i=0;i<chatRecords.size();i++)
        {
            ChatRecord chatRecord = chatRecords.get(i);
            int flag=1;
            //筛选sendId并去重
            if(!chatRecord.getSenderId().equals(patientId))
            {
                for(int j=0;j<num;j++)
                {
                    if(userId[j].equals(chatRecord.getSenderId()))
                    {
                        flag=0;
                    }
                }
                if(flag==1)
                {
                    userId[num]=chatRecord.getSenderId();
                    num++;
                }
            }
            flag=1;
            //筛选receiveId
            if(!chatRecord.getReceiverId().equals(patientId))
            {
                for(int j=0;j<num;j++)
                {
                    if(userId[j].equals(chatRecord.getReceiverId()))
                    {
                        flag=0;
                    }
                }
                if(flag==1)
                {
                    userId[num]=chatRecord.getReceiverId();
                    num++;
                }
            }
        }

        System.out.println("医生聊天列表数量:"+num);
        int KeFu = 1;
        //封装聊天列表，显示该医生最新的一条聊天记录
        ArrayList<PatientChatIndex> patientChatIndices = new ArrayList<>();
        for(int i=0;i<num;i++)
        {
            if(0==userId[i])
            {
                KeFu=0;
            }
            try {
                chatRecords = chatRecordMapper.selectNewestOneChat(patientId,userId[i]);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("showChatIndex 显示聊天列表错误");
            }
            ChatRecord chatRecord = chatRecords.get(0);
            PatientChatIndex patientChatIndex = new PatientChatIndex();
            //一条最新聊天记录
            patientChatIndex.setChatRecord(chatRecord);
            //该患者是否是发送方，并判断该条消息患者是否已读
            if(chatRecord.getSenderId().equals(patientId))
            {
                patientChatIndex.setFlag(1);
                patientChatIndex.setReadFlag(1);
            }
            else {
                patientChatIndex.setFlag(0);
                if(chatRecord.getReceiverState().equals("未读"))
                {
                    patientChatIndex.setReadFlag(0);
                }
                else {
                    patientChatIndex.setReadFlag(1);
                }
            }
            //把消息时间转化成字符串
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            patientChatIndex.setChatTime(simpleDateFormat.format(chatRecord.getChatTime()));
            try {
                //患者个人信息
                UserLoginForm userLoginForm = new UserLoginForm();
                userLoginForm = userLoginFormMapper.selectByPrimaryKey(patientId);
                patientChatIndex.setPatientName(userLoginForm.getUserName());
                patientChatIndex.setPatientHeadLink(userLoginForm.getLogoLink());
                //医生个人信息
                userLoginForm = userLoginFormMapper.selectByPrimaryKey(userId[i]);
                patientChatIndex.setDoctorId(userId[i]);
                patientChatIndex.setDoctorName(userLoginForm.getUserName());
                patientChatIndex.setDoctorHeadLink(userLoginForm.getLogoLink());


                patientChatIndices.add(patientChatIndex);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("showChatIndex 显示聊天列表错误 根据Id查询头像和名字错误");
            }

        }
        System.out.println("客服"+KeFu);
        if(KeFu==1||num==0)
        {
            try {
                PatientChatIndex patientChatIndex = new PatientChatIndex();
                UserLoginForm userLoginForm = new UserLoginForm();
                userLoginForm = userLoginFormMapper.selectByPrimaryKey(patientId);
                patientChatIndex.setPatientName(userLoginForm.getUserName());
                patientChatIndex.setPatientHeadLink(userLoginForm.getLogoLink());
                patientChatIndex.setDoctorId(0);
                //把消息时间转化成字符串
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                patientChatIndex.setChatTime(simpleDateFormat.format(new Date()));
                patientChatIndex.setIsImg("false");
                patientChatIndex.setIsRight("false");
                patientChatIndex.setReadFlag(1);
                patientChatIndex.setFlag(0);
                userLoginForm = userLoginFormMapper.selectByPrimaryKey(0);
                patientChatIndex.setDoctorName(userLoginForm.getUserName());
                patientChatIndex.setDoctorHeadLink(userLoginForm.getLogoLink());
                ChatRecord chatRecord = new ChatRecord();
                chatRecord.setFlag2(0);
                chatRecord.setChatContent("这里是峻宇牙科，欢迎您的咨询");
                chatRecord.setReceiverId(patientId);
                chatRecord.setSenderId(0);
                patientChatIndex.setChatRecord(chatRecord);
                patientChatIndices.add(patientChatIndex);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return patientChatIndices;
    }

    /**
     * @ description:患者在聊天里点击某医生，向患者展示聊天信息
     * @ param      :[patientId, doctorId]
     * @ return     :java.util.ArrayList<com.springboot.dto.PatientChatIndex>
     * @ date       :2020/12/30

    */
    @Override
    public ArrayList<PatientChatIndex> showPatientChatInfoByTwoId(Integer patientId, Integer doctorId) {
        ArrayList<ChatRecord> chatRecords = new ArrayList<>();
        try {
            System.out.println("patientId:"+patientId);
            System.out.println("doctorId:"+doctorId);
            chatRecords = chatRecordMapper.selectByTwoId(patientId,doctorId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 出错");
        }
        ArrayList<PatientChatIndex> patientChatIndices = new ArrayList<>();
        for(int i=0;i<chatRecords.size();i++)
        {
            ChatRecord chatRecord = chatRecords.get(i);
            UserLoginForm userLoginForm1 = new UserLoginForm();//患者
            UserLoginForm userLoginForm2 = new UserLoginForm();//医生
            PatientChatIndex patientChatIndex = new PatientChatIndex();
            patientChatIndex.setChatRecord(chatRecord);
            try {
                if(chatRecord.getSenderId().equals(patientId))
                {
                    userLoginForm1 = userLoginFormMapper.selectByPrimaryKey(patientId);
                    userLoginForm2 = userLoginFormMapper.selectByPrimaryKey(doctorId);
                }
                else {
                    userLoginForm2 = userLoginFormMapper.selectByPrimaryKey(patientId);
                    userLoginForm1 = userLoginFormMapper.selectByPrimaryKey(doctorId);
                }
                patientChatIndex.setPatientName(userLoginForm1.getUserName());
                patientChatIndex.setPatientHeadLink(userLoginForm1.getLogoLink());
                patientChatIndex.setDoctorName(userLoginForm2.getUserName());
                patientChatIndex.setDoctorHeadLink(userLoginForm2.getLogoLink());
                Integer flag = 1;
                if(chatRecord.getSenderId().equals(patientId))
                {
                    patientChatIndex.setIsRight("true");
                }
                else {
                    patientChatIndex.setIsRight("false");
                }

                if(chatRecord.getFlag2().equals(flag))
                {
                    patientChatIndex.setIsImg("true");
                }
                else {
                    patientChatIndex.setIsImg("false");
                }

                //设置时间格式
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                patientChatIndex.setChatTime(simpleDateFormat.format(chatRecord.getChatTime()));
                patientChatIndices.add(patientChatIndex);
            }
            catch (Exception e)
            {
                System.out.println("showPatientChatInfoByTwoId 错误");
                e.printStackTrace();
            }
        }

        //消息设置为已读
        try {
            int a = chatRecordMapper.patientSetReadChatByTwoId(patientId,doctorId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("showPatientChatInfoByTwoId 患者设置该医生已读消息错误");
        }
        return patientChatIndices;
    }

    /*
     * @ description:患者端添加一条聊天信息
     * @ param      :[chatRecord]
     * @ return     :int
     * @ date       :2021/1/1

    */
    @Override
    public int insertChatInfoByChatRecord(ChatRecord chatRecord) {
        int result = 0;
        try {
            result = chatRecordMapper.insertSelective(chatRecord);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 添加一条聊天信息失败");
        }
        return result;
    }

    /**
     * @ description:患者删除聊天记录
     * @ param      :[patientId, doctorId]
     * @ return     :int
     * @ date       :2021/1/3

    */
    @Override
    public int deleteChatInfo(Integer patientId, Integer doctorId) {
        int result = 0;
        try {
            result = chatRecordMapper.patientDeleteChatInfo(patientId, doctorId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 删除记录失败("+patientId+","+doctorId+")");
        }
        return result;
    }


}
